using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Product_Script : MonoBehaviour 
{
	private string name;
	private float weight;
	private float price;
	private int life;
	
	private List<Color> lifeColorsList = new List<Color>();
	
	public AudioClip touch_Sound;
	public AudioClip destroy_Sound;
	
	private bool killed = false;
	
	private void Start()
	{
		foreach(Transform child in transform)
		{
			child.GetComponent<TextMesh>().text = this.price.ToString();
		}
		
		this.createColors();
		this.audio.clip = this.touch_Sound;
	}
	
	public void Init(string newName, float newWeight, Color newColor, float newPrice, int newLife)
	{
		this.name = newName;
		this.weight = newWeight;
		//this.renderer.material.color = newColor;
		this.price = newPrice;
		this.life = newLife;
	}
	
	private void createColors()
	{
		this.lifeColorsList.Add(Color.white);
		this.lifeColorsList.Add(Color.red);
		this.lifeColorsList.Add(new Color(240f/255f, 140f/255f, 0f, 1f));
		this.lifeColorsList.Add(Color.yellow);
		this.lifeColorsList.Add(Color.cyan);
		this.lifeColorsList.Add(Color.blue);
	}
	
	// Update is called once per frame
	private void Update () 
	{
		this.transform.Translate(0, -(this.weight) * Time.deltaTime, 0);
	}
	
	public void Kill()
	{
		Destroy(this.gameObject);
	}
	
	IEnumerator KillElement()
	{
		this.transform.position = Camera.main.transform.position - new Vector3(0, 0, 25);
		
		this.audio.clip = this.destroy_Sound;
		this.audio.Play();
		yield return new WaitForSeconds(/*this.audio.clip.length*/1f);
		Destroy(this.gameObject);
	}
	
	public void Shoot(int damage)
	{
		this.life -= damage;
		
		if(this.life <= 0)
		{
			StartCoroutine(this.KillElement());
		}
		else
		{
			this.audio.Play();
			this.renderer.material.color = this.lifeColorsList[this.life];
		}
	}
	
	public float GetPrice()
	{
		return this.price;
	}
}
